<!--#include file="config.asp"-->
<%	
''' SDCMS 支付宝登录处理
''' ==================================================================
''' 版权所有 http://www.sdcms.cn
''' ------------------------------------------------------------------
''' 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用；
''' 未经授权不允许对程序代码以任何形式任何目的的再发布。
''' ==================================================================
''' 编写: IT平民
''' 修改：IT平民 in 2012.0

	sub callback()
		dim is_success:is_success=sdcms.enhtml(sdcms.fget("is_success",0))
		dim notify_id:notify_id=sdcms.enhtml(sdcms.fget("notify_id",0))
		dim real_name:real_name=sdcms.enhtml(sdcms.fget("real_name",0))
		dim token:token=sdcms.enhtml(sdcms.fget("token",0))
		dim user_id:user_id=sdcms.enhtml(sdcms.fget("user_id",0))
		dim sign:sign=sdcms.enhtml(sdcms.fget("sign",0))

		if sdcms.strlen(notify_id)=0 or sdcms.strlen(real_name)=0 or sdcms.strlen(token)=0 or sdcms.strlen(user_id)=0 or sdcms.strlen(sign)=0 then
			sdcms.echo "非法参数传递，无法登录"
			exit sub
		end if
		dim states'检查远程状态
		states=sdcms.gethttp("http://notify.alipay.com/trade/notify_query.do?partner="&alipay_appid&"&notify_id="&notify_id,"")
		dim thissign
		thissign=MD5_32("is_success="&is_success&"&notify_id="&notify_id&"&real_name="&real_name&"&token="&token&"&user_id="&user_id&alipay_appkey)
		if not(thissign=sign and states="true") then
			sdcms.echo "登录验证失败，无法登录"
			exit sub
		else
			sdcms.setsession "alipay_user_id",user_id
			dim gourl:gourl=sdcms.loadsession("api_backurl")
			dim data:data=sdcms.db.dbload(1,"n.userid,u.logintimes","sd_user_bind n left join sd_user u on n.userid=u.id","n.openid='"&user_id&"' and n.opentype='alipay'","")
			if ubound(data)<0 then
				sdcms.setsession "api_backurl",""
				sdcms.go "bind.asp?api_backurl="&gourl&"&nickname="&server.urlencode(real_name)
				exit sub
			else
				sdcms.db.dbupdate "sd_user","id="&data(0,0)&"",array(array("logintimes",data(1,0)+1,0,0))
				sdcms.resetuserinfo("u.id="&data(0,0)&"")
				sdcms.setsession "alipay_token",""
				sdcms.setsession "api_backurl",""
				if sdcms.strlen(gourl)=0 then gourl=webroot
				sdcms.go gourl
			end if
		end if
	end sub

	callback()
%>